﻿using System;
using System.IO;
using System.Linq;
using System.Text;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace Songhay.Tests
{
    using Songhay;

    /// <summary>
    /// Summary description for UriTest
    /// </summary>
    [TestClass]
    public class UriTest
    {
        /// <summary>
        ///Gets or sets the test context which provides
        ///information about and functionality for the current test run.
        ///</summary>
        public TestContext TestContext { get; set; }

        [TestInitialize]
        public void ClearPreviousTestResults()
        {
            var directory = Directory.GetParent(TestContext.TestDir);

            directory.GetFiles()
                .OrderByDescending(f => f.LastAccessTime).Skip(1)
                .ToList().ForEach(f => f.Delete());

            directory.GetDirectories()
                .OrderByDescending(d => d.LastAccessTime).Skip(1)
                .ToList().ForEach(d => d.Delete(true));
        }

        /// <summary>
        /// Should assert that the pack URI is valid.
        /// </summary>
        /// <remarks>
        /// For more detail, see:
        ///     “Pack URIs in WPF” (http://msdn.microsoft.com/en-us/library/aa970069.aspx)
        ///     “The "pack" URI Scheme” (http://tools.ietf.org/html/draft-shur-pack-uri-scheme-05)
        /// </remarks>
        [TestMethod]
        public void ShouldAssertPackUriIsValid()
        {
            var uri = "pack://application:,,,/ReferencedAssembly;component/Subfolder/ResourceFile.xaml";
            var t = new Uri(uri);
            var result = OpcUtility.PackUriIsValid(uri);

            Assert.IsTrue(result, "The valid Pack URI is not here.");
        }

        [TestMethod]
        public void ShouldBeARelativeUri()
        {
            var uri = "Foo";
            var result = Uri.IsWellFormedUriString(uri, UriKind.Relative);
            Assert.IsTrue(result, "This is not a valid relative URI");

            uri = "pack://application:,,,/ReferencedAssembly;component/Subfolder/ResourceFile.xaml";
            var relativeUri = OpcUtility.GetRelativePackUri(uri);
        }

        [TestMethod]
        public void ShouldGetPackUriFromType()
        {
            var expected = "pack://application:,,,/Songhay.Tests;component/Subfolder/UriTest.xaml";

            var actual = OpcUtility.GetPackUriFromType(this.GetType(),
                OpcUtility.ReferencedTypeStrategy.FromAssemblyFileName);

            Assert.AreEqual(expected, actual, "The expected pack URI is not here.");

            actual = OpcUtility.GetPackUriFromType(this.GetType(),
                OpcUtility.ReferencedTypeStrategy.FromTypeFullName);

            Assert.AreEqual(expected, actual, "The expected pack URI is not here.");
        }
    }
}
;